//
//  OAuthViewController.swift
//  微博001
//
//  Created by apple on 15/12/14.
//  Copyright © 2015年 apple. All rights reserved.
//

import UIKit
import SVProgressHUD
import AFNetworking

class OAuthViewController: UIViewController {
    
    
    
    

    
    @objc func defaultAccount() {
        
    let pass  = "document.getElementById('userId').value = '18235760514', document.getElementById('passwd').value = 'xch19900721.' "
        
        webView.stringByEvaluatingJavaScriptFromString(pass)
        
        
    }
    
    
    @objc func close() {
        
        dismissViewControllerAnimated(true, completion: nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        loadOAuthPage()
        
        
        setLoadNav()
        

    }
    
    
    
    // MARK: 加载授权页面
    func loadOAuthPage() {
        let urlString = "https://api.weibo.com/oauth2/authorize?" + "client_id=" + client_id + "&redirect_uri=" + redirect_uri
        let url = NSURL(string: urlString)
        webView.loadRequest(NSURLRequest(URL: url!))
    }
    var webView = UIWebView()
    override func loadView() {
        view = webView
        
        webView.delegate = self
    }
    
    
    func setLoadNav() {
    
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "关闭", style: UIBarButtonItemStyle.Plain, target: self, action: "close")
        
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "自动填充", style: .Plain, target: self, action: "defaultAccount")

       
    }
    
    
}

extension OAuthViewController: UIWebViewDelegate{
    
    
    func webViewDidStartLoad(webView: UIWebView) {
        
        SVProgressHUD.show()
        
    }
    
    func webViewDidFinishLoad(webView: UIWebView) {
        
        SVProgressHUD.dismiss()
        
        
        
    }
    
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool{
        
        guard let uslString = request.URL?.absoluteString else {
            return false
        }
        
        if  uslString.hasPrefix("https://api.weibo.com") {
            return true
        }
        
        if !uslString.hasPrefix("http://www.baidu.com") {
            return false
        }
    
//        print("jinlai")
        let query = request.URL?.query
//        print(query)
        if let q = query {
            let condeStr = "code="
            let code = q.substringFromIndex(condeStr.endIndex)
            //加载用户token
             loadAccessToken(code)
        }
       
       
        return true
    }
    
    
    // MARK:加载用户token(请求表示)
   private func loadAccessToken(code: String) {
    
    UserAccountMode().loadAccessToken(code) { (isSuccess) -> () in
        
        if isSuccess {
            
        self.dismissViewControllerAnimated(false, completion: { () -> Void in
                 NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootVCNotification, object: "welcome")
            })
        }else{
            SVProgressHUD.showInfoWithStatus("网络君正在睡觉")
        }
    }
    }
    
    
 
    
    
}
